const api = require('./api.js');
var API = require('./api.js');
function request(apiName, data, callback) {
  var _this =this;
  if (typeof callback !== "function") {
    callback = function () { }
  }
  var url = API.apiUrl + apiName.api;
  var method = apiName.method;
  wx.request({
    url: url,
    method: method,
    header: {
      'X-Requested-With': 'XMLHttpRequest',
      'Content-Type': 'application/json',
      'EnterpriseId':' 1378190227727616',
      'GroupId': '1378190227727616',
      "Authorization": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjU3MjMyNzIsInVzZXJuYW1lIjoiMTMyNjcxMjI1MjQifQ.ERLQIYmBu147wA8CgzXC2zO-f4PeFbjHLwNd32K5toY",
      "Cookie": "cookie_username_132671225242=ff7f84c3-5b73-4035-88ea-a3aa690781f1;SESSION=YjcxOWE5MTUtMDlmMC00Y2NiLWI0MjQtZTgwMzJkNjg5NmU3"
    },
    data: data,
    success(res) {
      if (res.statusCode === 200) {
        callback({ result: res.data });
      }else {
        if (res.statusCode== 401) {
          getApp().login();
        } else {
          console.log(`错误url:${url},data:${data}`)
          wx.showToast({
            title: '网络错误请稍后重试！'
          })
          return
        }
        callback({ error: true });
      }
    },
    fail(err) {
      wx.showToast({
        title: '服务器出错请稍后再试！'
      })
      callback({ error: true });
    }
  });
};
// 设定滑块高度
function scrollheight(_this, ...argument) {
  let radio = 750 / wx.getSystemInfoSync().windowWidth
  let sum = (...argument) => {
    let total = 0;
    argument.forEach(x => {
      total += x
    })
    return total * radio
  }
  let scrollHeight = wx.getSystemInfoSync().windowHeight * radio - sum(...argument) + 'rpx';
  _this.setData({
    scrollHeight
  })
};
//计算列表页数 
 function getTotalPageNum(totalCount, pageSize) {
  var p = totalCount / pageSize; if (totalCount % pageSize == 0)
    return p;
  else
    return parseInt(p + 1);
};
function getvl(url,name) { //截取视频vid参数
  var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
  if (reg.test(url)) return unescape(RegExp.$2.replace(/\+/g, " "));
  return "";
};
module.exports = {
  request: request,
  scrollheight: scrollheight,
  getTotalPageNum:getTotalPageNum,
  API: api,
  getvl: getvl
}
